package ee.rsi2m.goproject.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import ee.rsi2m.goproject.model.GoStone;

import android.graphics.Canvas;
import android.graphics.Color;

public class StoneListHolder {
	private List<GoStone> stones;

	public StoneListHolder() {
		stones = Collections.synchronizedList(new ArrayList<GoStone>());
	}

	public void addStone(GoStone stone) {
		stones.add(stone);
	}

	public List<GoStone> getStones() {
		return stones;
	}

	public void setStones(List<GoStone> stones) {
		stones = stones;
	}

	public void undo() {
		final int length = currentStackLength();
		if (length > 0) {
			final GoStone undoCommand = stones.get(length - 1);
			stones.remove(length - 1);
			undoCommand.undo();
		}
	}

	public int currentStackLength() {
		final int length = stones.toArray().length;
		return length;
	}

	public void executeAll(Canvas canvas) {

		if (stones != null) {
			synchronized (stones) {
				final Iterator<GoStone> i = stones.iterator();
				while (i.hasNext()) {
					GoStone drawingPath = (GoStone) i.next();
					if (drawingPath.isVisibilible()) {
						drawingPath.draw(canvas);
					}
				}
			}
		}
	}

	public boolean hasMoreUndo() {
		return stones.toArray().length > 0;
	}
}
